package com.weiquan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.weiquan.entity.SysAdmin;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 系统管理员Mapper接口
 */
@Mapper
public interface SysAdminMapper extends BaseMapper<SysAdmin> {

    /**
     * 根据用户名查询管理员
     */
    @Select("SELECT * FROM sys_admin WHERE username = #{username} AND is_deleted = 0")
    SysAdmin selectByUsername(@Param("username") String username);

    /**
     * 根据手机号查询管理员
     */
    @Select("SELECT * FROM sys_admin WHERE phone_number = #{phoneNumber} AND is_deleted = 0")
    SysAdmin selectByPhoneNumber(@Param("phoneNumber") String phoneNumber);

    /**
     * 根据邮箱查询管理员
     */
    @Select("SELECT * FROM sys_admin WHERE email = #{email} AND is_deleted = 0")
    SysAdmin selectByEmail(@Param("email") String email);

    /**
     * 更新最后登录信息
     */
    @Update("UPDATE sys_admin SET last_login_time = #{loginTime}, last_login_ip = #{loginIp}, login_count = login_count + 1 WHERE id = #{adminId}")
    int updateLastLoginInfo(@Param("adminId") Long adminId, @Param("loginTime") LocalDateTime loginTime, @Param("loginIp") String loginIp);

    /**
     * 根据角色查询管理员列表
     */
    @Select("SELECT * FROM sys_admin WHERE role = #{role} AND is_deleted = 0 ORDER BY create_time DESC")
    List<SysAdmin> selectByRole(@Param("role") Integer role);

    /**
     * 查询启用状态的管理员
     */
    @Select("SELECT * FROM sys_admin WHERE status = 1 AND is_deleted = 0 ORDER BY create_time DESC")
    List<SysAdmin> selectActiveAdmins();

    /**
     * 统计各角色管理员数量
     */
    @Select("SELECT role, COUNT(*) as count FROM sys_admin WHERE is_deleted = 0 GROUP BY role")
    List<Map<String, Object>> countByRole();

    /**
     * 查询最近登录的管理员
     */
    @Select("SELECT * FROM sys_admin WHERE last_login_time IS NOT NULL AND is_deleted = 0 ORDER BY last_login_time DESC LIMIT #{limit}")
    List<SysAdmin> selectRecentLogin(@Param("limit") Integer limit);

    /**
     * 根据权限查询管理员
     */
    @Select("SELECT * FROM sys_admin WHERE permissions LIKE CONCAT('%', #{permission}, '%') AND is_deleted = 0")
    List<SysAdmin> selectByPermission(@Param("permission") String permission);

    /**
     * 批量更新管理员状态
     */
    @Update("UPDATE sys_admin SET status = #{status}, update_time = NOW() WHERE id IN (${adminIds}) AND is_deleted = 0")
    int batchUpdateStatus(@Param("adminIds") String adminIds, @Param("status") Integer status);

    /**
     * 查询管理员登录统计
     */
    @Select("SELECT DATE(last_login_time) as login_date, COUNT(*) as login_count FROM sys_admin WHERE last_login_time >= #{startDate} AND last_login_time <= #{endDate} AND is_deleted = 0 GROUP BY DATE(last_login_time) ORDER BY login_date DESC")
    List<Map<String, Object>> getLoginStatistics(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate);
}